<!DOCTYPE html><html lang="en" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>支持向量机（下） | Bulua</title><meta name="keywords" content="svm, 支持向量机"><meta name="author" content="Bulua"><meta name="copyright" content="Bulua"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="上篇仅介绍了SVM的基本概念，本篇着重讲解SVM中的最佳线性分类器（最大边界超平面）是如何求得的。">
<meta property="og:type" content="article">
<meta property="og:title" content="支持向量机（下）">
<meta property="og:url" content="http://bulua.gitee.io/2021/10/12/svm-xia/index.html">
<meta property="og:site_name" content="Bulua">
<meta property="og:description" content="上篇仅介绍了SVM的基本概念，本篇着重讲解SVM中的最佳线性分类器（最大边界超平面）是如何求得的。">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://gitee.com/bulua/bulua_img/raw/master/download.jpg">
<meta property="article:published_time" content="2021-10-12T00:31:00.000Z">
<meta property="article:modified_time" content="2021-10-12T00:37:05.787Z">
<meta property="article:author" content="Bulua">
<meta property="article:tag" content="机器学习">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/bulua/bulua_img/raw/master/download.jpg"><link rel="shortcut icon" href="https://gitee.com/bulua/bulua_img/raw/master/tubiao.jpg"><link rel="canonical" href="http://bulua.gitee.io/2021/10/12/svm-xia/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: 'days',
  date_suffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"Traditional Chinese Activated Manually","cht_to_chs":"Simplified Chinese Activated Manually","day_to_night":"Dark Mode Activated Manually","night_to_day":"Light Mode Activated Manually","bgLight":"#49b1f5","bgDark":"#121212","position":"top-center"},
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isanchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '支持向量机（下）',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-10-12 08:37:05'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    document.addEventListener('pjax:complete', detectApple)})(window)</script><link rel="stylesheet" href="/css/custom.css" media="defer" onload="this.media='all'"><meta name="generator" content="Hexo 5.4.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/tubiao.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">8</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">Tags</div><div class="length-num">3</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> About</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gitee.com/bulua/bulua_img/raw/master/download.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Bulua</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> About</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">支持向量机（下）</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2021-10-12T00:31:00.000Z" title="Created 2021-10-12 08:31:00">2021-10-12</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2021-10-12T00:37:05.787Z" title="Updated 2021-10-12 08:37:05">2021-10-12</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="支持向量机（下）"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>上篇仅介绍了SVM的基本概念，本篇着重讲解SVM中的最佳线性分类器（最大边界超平面）是如何求得的。 </p>
<span id="more"></span>

<h2 id="4-求解超平面"><a href="#4-求解超平面" class="headerlink" title="4.求解超平面"></a>4.求解超平面</h2><h2 id="4-1几何间隔"><a href="#4-1几何间隔" class="headerlink" title="4.1几何间隔"></a>4.1几何间隔</h2><p>上一小节给出<strong>二维问题下最佳线性分割的标准</strong>，<strong>就是分割线到两类边界点的距离最“宽”</strong>，那么这个“宽度”怎么量化和求解呢？</p>
<p><img src= "" data-lazy-src="https://pic1.zhimg.com/80/v2-7620f1da2880316db9606ed01f60b444_720w.jpg" alt="img"></p>
<p>我们知道，点 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=(x_%7B0%7D+,y_%7B0%7D)" alt="[公式]"> 直线Ax+By+c=0的距离（中学的知识点），可以表示为：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=D=%5Cfrac%7B%7CAx_%7B0%7D+By_%7B0%7D+c%7C%7D+%7B%5Csqrt%7BA%5E%7B2%7D+B%5E%7B2%7D%7D%7D" alt="[公式]"></p>
<p>在我们的二维问题中，第i个点的坐标为 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=X_%7Bi%7D=(x_%7Bi1%7D,x_%7Bi2%7D)%5E%7BT%7D" alt="[公式]"> ，直线为 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=w_%7B1%7Dx_%7B1%7D+w_%7B2%7Dx_%7B2%7D+b=W%5E%7BT%7DX+b=0" alt="[公式]"> （为了打公式方便，后面不区分向量和其转置，省略T标志，统一写成 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=WX+b=0" alt="[公式]"> ），将上式替换， <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=X_%7Bi%7D" alt="[公式]"> 到分割直线的距离为：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=D=%5Cfrac%7B%7CWX_%7Bi%7D+b%7C%7D+%7B%5Csqrt%7Bw_%7B1%7D%5E%7B2%7D+w_%7B2%7D%5E%7B2%7D%7D%7D=%5Cfrac%7B%7CWX_%7Bi%7D+b%7C%7D+%7B%7C%7CW%7C%7C%7D" alt="[公式]"></p>
<p>有的人也许对分母||W||感到陌生，这里多做点解释。</p>
<p>||W||是向量W的2-范数（ <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=L_%7B2%7D" alt="[公式]"> 范数），一般我们说<strong>向量长度</strong>，<strong>指的是向量的2-范数</strong>。例如这里的 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=W=(w_%7B1%7D,w_%7B2%7D)" alt="[公式]"> ，它的2-范数就是 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%7C%7CW%7C%7C_%7B2%7D=%5Csqrt%7Bw_%7B1%7D%5E%7B2%7D+w_%7B2%7D%5E%7B2%7D%7D" alt="[公式]"> （通常会省略下标2，一般说||W||就是指 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%7C%7CW%7C%7C_%7B2%7D" alt="[公式]"> <em>）</em>，而它的p-范数（ <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=L%7Bp%7D" alt="[公式]"> 范数）就是 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%7C%7CW%7C%7C_%7Bp%7D=%5Csqrt%5Bp%5D%7Bw_%7B1%7D%5E%7Bp%7D+w_%7B2%7D%5E%7Bp%7D%7D" alt="[公式]"> 。</p>
<p>这里给出向量范数的一般形式。对于n维向量 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=W=(w_%7B1%7D,w_%7B2%7D,...,w_%7Bn%7D)" alt="[公式]"> ，它的<strong>p-范数</strong>为：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%7C%7CW%7C%7C%7Bp%7D=%5Csqrt%5Bp%5D%7Bw%7B1%7D%5E%7Bp%7D+w_%7B2%7D%5E%7Bp%7D+...+w_%7Bn%7D%5E%7Bp%7D%7D" alt="[公式]"></p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=D=%5Cfrac%7B%7CWX_%7Bi%7D+b%7C%7D+%7B%7C%7CW%7C%7C%7D" alt="[公式]"> 这个公式的学名叫做<strong>几何间隔</strong>，几何间隔表示的是点到超平面的<strong>欧氏距离</strong>（还记得上次讲线性回归时强调要记住这个名称吧？）。</p>
<p>以上是单个点到某个超平面的距离定义（在这个具体的二维例子中是一条直线，我们认为直线也是属于超平面的一种，后面统一写超平面的时候，不要觉得混乱哦）。上一节我们说要求最宽的“宽度”，最厚的“厚度”，其实就是<strong>求支持向量到超平面的几何间隔最大值</strong>。</p>
<p>回到二维问题，令“马路宽度”为2d，即最佳分割线到两类支持向量的距离均为d，最佳分割线参数的求解目标就是使d最大。</p>
<p><img src= "" data-lazy-src="https://pic3.zhimg.com/80/v2-09de7799a0985ec86cae67738496bfd6_720w.jpg" alt="img"></p>
<h2 id="4-2凸二次规划"><a href="#4-2凸二次规划" class="headerlink" title="4.2凸二次规划"></a>4.2凸二次规划</h2><p>假设L1是其中一条过+1类支持向量且平行于 WX+b=0的直线，L2是过-1类支持向量且平行于 WX+b=0的直线，这L1和L2就确定了这条马路的边边，L是马路中线。</p>
<p>由于确定了L的形式是 WX+b=0，又因为L1、L2距离L是相等的，我们定义L1为WX+b=1，L2为WX+b=-1。为什么这两条平行线的方程右边是1和-1？其实也可以是2和-2，或者任意非零常数C和-C，规定1和-1只是为了方便。就像2x+3y+1=0和4x+6y+2=0是等价的，方程同乘一个常数后并不会改变。</p>
<p><img src= "" data-lazy-src="https://pic4.zhimg.com/80/v2-999aae9790e3f5e67b9656733000093f_720w.jpg" alt="img"></p>
<p>确定了三条线的方程，我们就可以求马路的宽度2d。2d是L1和L2这两条平行线之间的距离：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=2d=%5Cfrac%7B%7C1-(-1)%7C%7D%7B%7C%7CW%7C%7C%7D=%5Cfrac%7B2%7D%7B%7C%7CW%7C%7C%7D" alt="[公式]"></p>
<p>4.1小节我们讲了现在的目标是<strong>最大化几何间隔d</strong>，由于 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=d=%5Cfrac%7B1%7D%7B%7C%7CW%7C%7C%7D" alt="[公式]"> ，问题又转化成了最小化||W||。对于求min||W||，通常会转化为求 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=min%5Cfrac%7B%7C%7CW%7C%7C%5E%7B2%7D%7D%7B2%7D" alt="[公式]"> ，为什么要用平方后除以2这个形式呢？这是为了后面求解方便（还记得上次讲线性回归讲到的凸函数求导吗？）。</p>
<p>||W||不可能无限小，因为还有限制条件呢。<strong>超平面正确分类意味着点i到超平面的距离恒大于等于d</strong>，即:</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Cfrac%7B%7CWX_%7Bi%7D+b%7C%7D%7B%7C%7CW%7C%7C%7D%5Cgeq+d=%5Cfrac%7B1%7D%7B%7C%7CW%7C%7C%7D" alt="[公式]"></p>
<p>两边同乘||W||，简化为：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%7CWX_%7Bi%7D+b%7C=y_%7Bi%7D(WX_%7Bi%7D+b)%5Cgeq+1" alt="[公式]"></p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%7CWX_%7Bi%7D+b%7C=y_%7Bi%7D(WX_%7Bi%7D+b)" alt="[公式]"> 应该不难理解吧？因为y只有两个值，+1和-1。 - 如果第i个样本属于+1类别， <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=y_%7Bi%7D%3E0" alt="[公式]"> ，同时 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=WX_%7Bi%7D+b%3E0" alt="[公式]"> ，两者相乘也大于0； - 若属于-1类， <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=y_%7Bi%7D%3C0" alt="[公式]"> ， <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=WX_%7Bi%7D+b%3C0" alt="[公式]"> ，此时相乘依旧是大于0的。</p>
<p>这意味着 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=y_%7Bi%7D(WX_%7Bi%7D+b)" alt="[公式]"> 恒大于0，y起到的作用就和绝对值一样。之所以要用y替换绝对值，是因为y是已知样本数据，方便后面的公式求解。</p>
<p>我们将这个目标规划问题用数学语言描述出来。</p>
<p><strong>目标函数</strong>：</p>
<p><img src= "" data-lazy-src="https://pic3.zhimg.com/80/v2-cd9c4c1cb511af3f62ce96b88b461e22_720w.png" alt="img"></p>
<p><strong>约束条件</strong>：</p>
<p><img src= "" data-lazy-src="https://pic2.zhimg.com/80/v2-89a910f53ffd1d31961417874e8f6671_720w.png" alt="img"></p>
<p>这里约束条件是W的多个线性函数（n代表样本个数），目标函数是W的二次函数（再次提醒，X、y不是变量，是已知样本数据），这种规划问题叫做<strong>凸二次规划</strong>。</p>
<p>什么叫凸二次规划？之前讲线性回归最小二乘法时，提到了处处连续可导且有最小值的凸函数。从二维图形上理解“凸”，就是在一个“凸”形中，任取其中两个点连一条直线，这条线上的点仍然在这个图形内部，例如 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=f(x)=x%5E%7B2%7D" alt="[公式]"> 上方的区域。</p>
<p>当一个约束优化问题中，目标函数和约束函数是凸函数（线性函数也是凸函数），该问题被称为<strong>凸优化问题</strong>。 当凸优化问题中的目标函数是一个二次函数时，就叫做凸二次规划，一般形式为：</p>
<p><img src= "" data-lazy-src="https://pic3.zhimg.com/80/v2-83509e73eba46a2b92de8e2c92219362_720w.jpg" alt="img"></p>
<p>若上式中的Q为正定矩阵，则目标函数有唯一的全局最小值。我们的问题中，Q是一个单位矩阵。</p>
<h2 id="4-3拉格朗日乘数法"><a href="#4-3拉格朗日乘数法" class="headerlink" title="4.3拉格朗日乘数法"></a>4.3拉格朗日乘数法</h2><p>这种不等式条件约束下的求多元函数极值的问题，到底怎么求解呢？</p>
<p>【<em>以下涉及高等数学知识，推导过程较为复杂，数学基础较弱或对推导没兴趣的同学可以跳过，直接看第5小节</em>】</p>
<p>当我们求一个函数 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=f(x,y)" alt="[公式]"> 的极值时，如果不带约束，分别对x，y求偏导，令两个偏导等于0，解方程即可。这种是求<strong>无条件极值</strong>。</p>
<p>带约束的话则为<strong>条件极值</strong>，如果约束为等式，有时借助换元法可以将有条件转化为无条件极值从而求解，不过换元消元也只能解决三元以内的问题。而<strong>拉格朗日乘数法</strong>可以通过引入新的未知标量（<strong>拉格朗日乘数</strong> <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Clambda" alt="[公式]"> ），直接求多元函数条件极值，不必先把问题转化为无条件极值的问题。</p>
<p>求函数f(x,y)在附加条件 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Cvarphi(x,y)=0" alt="[公式]"> 下可能的极值点，可以先做拉格朗日函数：</p>
<p><img src= "" data-lazy-src="https://pic3.zhimg.com/80/v2-1486a9904af157cffab6599808417896_720w.png" alt="img"></p>
<p>讲完拉格朗日乘数法的思路，仍解决不了上面的问题，因为该问题约束条件是不等式。其实，解决办法很简单，分成两部分看就行。</p>
<p>为了看起来简洁，我们令目标函数 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=min%5Cfrac%7B%7C%7CW%7C%7C%5E%7B2%7D%7D%7B2%7D" alt="[公式]"> 为 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=minf(W)" alt="[公式]"> ，约束条件 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=y_%7Bi%7D(WX_%7Bi%7D+b)-1%5Cgeq+0" alt="[公式]"> 为 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=g_%7Bi%7D(W)%5Cgeq+0" alt="[公式]"> 。</p>
<ul>
<li>当可行解W落在 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=g_%7Bi%7D(W)%3E0" alt="[公式]"> 区域内时，此时的问题就是求无条件极值问题（因为极小值已经包含在整个大区域内），直接极小化 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=f(W)" alt="[公式]"> 就行；</li>
<li>当可行解W落在 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=g_%7Bi%7D(W)=0" alt="[公式]"> 区域内时，此时的问题就是等式约束下的求极值问题，用拉格朗日乘数法求解即可。</li>
</ul>
<p>这两部分对应到样本上，又是什么？</p>
<ul>
<li>当可行解落在 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=g_%7Bi%7D(W)%3E0" alt="[公式]"> 内时，此时i这个样本点是“马路”之外的点；</li>
<li>当可行解落在 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=g_%7Bi%7D(W)=0" alt="[公式]"> 内时，此时i这个样本点正好落在马路边上，也就是我们的支持向量！</li>
</ul>
<p>我们再进一步思考下：</p>
<ul>
<li>当可行解落在 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=g_%7Bi%7D(W)%3E0" alt="[公式]"> 内时，此时约束不起作用（即求无条件极值），也就是拉格朗日乘数 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Clambda_%7Bi%7D=0" alt="[公式]"> ；</li>
<li>当可行解落在边界上时， <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Clambda_%7Bi%7D%5Cneq+0+%EF%BC%8Cg_%7Bi%7D(W)=0" alt="[公式]"> 。</li>
</ul>
<p>不论是以上哪种情况， <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Clambda_%7Bi%7D+g_%7Bi%7D(W)=0" alt="[公式]"> 均成立。</p>
<p>搞懂了上面说的，接下来构造拉格朗日函数，n个不等式约束都要加拉格朗日函数 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Clambda_%7Bi%7D+%5Cgeq+0" alt="[公式]"> ，有拉格朗日乘数向量 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Clambda+=(%5Clambda_%7B1%7D,...%5Clambda_%7Bn%7D)%5E%7BT%7D" alt="[公式]"> ：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=L(W,b,%5Clambda+)+=+f(W)-%5Csum_%7B1%7D%5E%7Bn%7D+%5Clambda_%7Bi%7D+g_%7Bi%7D(W)++=%5Cfrac%7B%7C%7CW%7C%7C%5E%7B2%7D%7D%7B2%7D+-+%5Csum_%7B1%7D%5E%7Bn%7D++%5Clambda_%7Bi%7Dy_%7Bi%7D(WX_%7Bi%7D+b)+++%5Csum_%7B1%7D%5E%7Bn%7D++%5Clambda_%7Bi%7D+%5Ctag%7B1%7D" alt="[公式]"></p>
<p>要求极值，先对参数求偏导：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Cfrac%7B%5Cpartial+L%7D%7B%5Cpartial+W%7D=W-%5Csum_%7B1%7D%5E%7Bn%7D++%5Clambda_%7Bi%7Dy_%7Bi%7DX_%7Bi%7D=0+%5Ctag%7B2%7D" alt="[公式]"></p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Cfrac%7B%5Cpartial+L%7D%7B%5Cpartial+b%7D=%5Csum_%7B1%7D%5E%7Bn%7D++%5Clambda_%7Bi%7Dy_%7Bi%7D=0+%5Ctag%7B3%7D" alt="[公式]"></p>
<p>此时又要引入一个概念——对偶问题（可见学明白SVM需要的数学知识真的很多，怪不得是机器学习入门的拦路虎呢），关于对偶问题，可以参见<a href="https://link.zhihu.com/?target=https://blog.csdn.net/qq_34564612/article/details/79974635">这篇文章</a>，这里不赘述。简单而言，就是原先的问题是先求L对 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Clambda" alt="[公式]"> 的max再求对W、b的min，变成先求L对W、b的min再求<img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Clambda" alt="[公式]">的max。</p>
<p>将(2)式 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=W=%5Csum_%7B1%7D%5E%7Bn%7D++%5Clambda_%7Bi%7Dy_%7Bi%7DX_%7Bi%7D" alt="[公式]"> 代入(1)式，得：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=L+=+%5Cfrac%7B1%7D%7B2%7D(%5Csum_%7B1%7D%5E%7Bn%7D++%5Clambda_%7Bi%7Dy_%7Bi%7DX_%7Bi%7D)(%5Csum_%7B1%7D%5E%7Bn%7D++%5Clambda_%7Bi%7Dy_%7Bi%7DX_%7Bi%7D)+-++%5Csum_%7B1%7D%5E%7Bn%7D++%5Clambda_%7Bi%7Dy_%7Bi%7D+((%5Csum_%7B1%7D%5E%7Bn%7D++%5Clambda_%7Bi%7Dy_%7Bi%7DX_%7Bi%7D)X_%7Bi%7D+b)+%5Csum_%7B1%7D%5E%7Bn%7D++%5Clambda_%7Bi%7D+" alt="[公式]"></p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex==+%5Csum_%7B1%7D%5E%7Bn%7D++%5Clambda_%7Bi%7D+-+%5Cfrac%7B1%7D%7B2%7D(%5Csum_%7Bi=1%7D%5E%7Bn%7D+%5Csum_%7Bj=1%7D%5E%7Bn%7D++%5Clambda_%7Bi%7D+%5Clambda_%7Bj%7Dy_%7Bi%7Dy_%7Bj%7DX_%7Bi%7D%5E%7BT%7DX_%7Bj%7D)++%5Ctag%7B4%7D" alt="[公式]"></p>
<p>此时目标函数中W、b就都被消去了，约束最优化问题就转变成： <strong>目标函数：</strong></p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=minL(%5Clambda+)+=+min(%5Cfrac%7B1%7D%7B2%7D%5Csum_%7Bi=1%7D%5E%7Bn%7D+%5Csum_%7Bj=1%7D%5E%7Bn%7D++%5Clambda_%7Bi%7D+%5Clambda_%7Bj%7Dy_%7Bi%7Dy_%7Bj%7DX_%7Bi%7D%5E%7BT%7DX_%7Bj%7D-%5Csum_%7B1%7D%5E%7Bn%7D++%5Clambda_%7Bi%7D+)+%5Ctag%7B5%7D" alt="[公式]"></p>
<p><strong>约束条件：</strong></p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Csum_%7B1%7D%5E%7Bn%7D++%5Clambda_%7Bi%7Dy_%7Bi%7D=0+,+%5Clambda_%7Bi%7D+%5Cgeq+0+%5Ctag%7B6%7D" alt="[公式]"></p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Clambda_%7Bi%7D(+y_%7Bi%7D(WX_%7Bi%7D+b)-1)+=+0,+i=1,2...n+%5Ctag%7B7%7D" alt="[公式]"></p>
<p>(6)式来自上面的(3)式，(7)式是由 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Clambda_%7Bi%7D+g_%7Bi%7D(W)=0" alt="[公式]"> 而来。如果能解出上面这个问题的最优解，我们就可以根据这个 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Clambda" alt="[公式]"> 求解出我们需要的W和b。</p>
<h2 id="5-线性可分问题小结"><a href="#5-线性可分问题小结" class="headerlink" title="5.线性可分问题小结"></a>5.线性可分问题小结</h2><p>最大边界超平面是由支持向量决定的，支持向量是边界上的样本点：</p>
<ul>
<li>假设有m个支持向量，则 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=W=%5Csum_%7B1%7D%5E%7Bm%7D++%5Clambda_%7Bi%7Dy_%7Bi%7DX_%7Bi%7D" alt="[公式]"> ；</li>
<li>从m个支持向量中任选一个，可以求 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=b=y_%7Bi%7D-WX_%7Bi%7D" alt="[公式]"> ；</li>
<li>决策函数 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=h(x)=Sign(W%5E%7BT%7DX+b)=Sign%5B%5Csum_%7B1%7D%5E%7Bm%7D++(%5Clambda_%7Bi%7Dy_%7Bi%7DX_%7Bi%7D%5E%7BT%7D)X+b%5D" alt="[公式]"> ；</li>
<li>对新样本 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=X_%7B*%7D" alt="[公式]"> 的预测： <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=Sign(W%5E%7BT%7DX_%7B*%7D+b)%EF%BC%8CW%5E%7BT%7DX_%7B*%7D+b" alt="[公式]"> 为正则 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=y_%7B*%7D=1" alt="[公式]"> ，为负则 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=y_%7B*%7D=-1" alt="[公式]"> ,为0拒绝判断。</li>
</ul>
<p>至此我们讲完了线性分类支持向量机的逻辑思想和求解过程，但这些只是SVM的基础知识，真正的核心其实还没有讲到。要知道，SVM的优势在于解决<strong>小样本</strong>、<strong>非线性</strong>和<strong>高维</strong>的回归和二分类问题（Support vector machine,SVM，1992， Boser, Guyon and Vapnik)。</p>
<ul>
<li>小样本，是指与问题的复杂度相比，SVM要求的样本数相对较少；</li>
<li>非线性，是指SVM擅长应付样本数据线性不可分的情况，主要通过<strong>核函数和松弛变量</strong>来实现，这一块才是SVM的精髓。由于这一系列文章是我的机器学习入门笔记，所以暂时不会涉及，等入门系列结束后也许会更深入地研究；</li>
<li>高维，是指样本维数很高，因为SVM 产生的分类器很简洁，用到的样本信息很少，仅仅用到支持向量。由于分类器仅由支持向量决定，SVM还能够<strong>有效避免过拟合</strong>。</li>
</ul>
<p>转载链接：<a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/74484361">https://zhuanlan.zhihu.com/p/74484361</a></p>
<p>原创博主：化简可得</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="mailto:undefined">Bulua</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="http://bulua.gitee.io/2021/10/12/svm-xia/">http://bulua.gitee.io/2021/10/12/svm-xia/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/">机器学习</a></div><div class="post_share"><div class="social-share" data-image="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2021/10/19/Kernel-Function/"><img class="prev-cover" src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">核函数</div></div></a></div><div class="next-post pull-right"><a href="/2021/10/12/svm-shang/"><img class="next-cover" src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">支持向量机（上）</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> Related Articles</span></div><div class="relatedPosts-list"><div><a href="/2021/10/19/Kernel-Function/" title="核函数"><img class="cover" src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-19</div><div class="title">核函数</div></div></a></div><div><a href="/2021/10/11/Logistic-Regression/" title="Logistic-Regression"><img class="cover" src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-11</div><div class="title">Logistic-Regression</div></div></a></div><div><a href="/2021/11/15/PCA/" title="PCA"><img class="cover" src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-11-15</div><div class="title">PCA</div></div></a></div><div><a href="/2021/11/15/LDA/" title="LDA"><img class="cover" src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-11-15</div><div class="title">LDA</div></div></a></div><div><a href="/2021/10/12/svm-shang/" title="支持向量机（上）"><img class="cover" src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-12</div><div class="title">支持向量机（上）</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> Comment</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/tubiao.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Bulua</div><div class="author-info__description">No matter how far you may fly, never forget where you come from.</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">8</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">Tags</div><div class="length-num">3</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/Bulua" target="_blank" title="Github"><i class="fab fa-github"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>Announcement</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-%E6%B1%82%E8%A7%A3%E8%B6%85%E5%B9%B3%E9%9D%A2"><span class="toc-number">1.</span> <span class="toc-text">4.求解超平面</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-1%E5%87%A0%E4%BD%95%E9%97%B4%E9%9A%94"><span class="toc-number">2.</span> <span class="toc-text">4.1几何间隔</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2%E5%87%B8%E4%BA%8C%E6%AC%A1%E8%A7%84%E5%88%92"><span class="toc-number">3.</span> <span class="toc-text">4.2凸二次规划</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-3%E6%8B%89%E6%A0%BC%E6%9C%97%E6%97%A5%E4%B9%98%E6%95%B0%E6%B3%95"><span class="toc-number">4.</span> <span class="toc-text">4.3拉格朗日乘数法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-%E7%BA%BF%E6%80%A7%E5%8F%AF%E5%88%86%E9%97%AE%E9%A2%98%E5%B0%8F%E7%BB%93"><span class="toc-number">5.</span> <span class="toc-text">5.线性可分问题小结</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/11/15/LDA/" title="LDA"><img src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="LDA"/></a><div class="content"><a class="title" href="/2021/11/15/LDA/" title="LDA">LDA</a><time datetime="2021-11-15T12:54:12.000Z" title="Created 2021-11-15 20:54:12">2021-11-15</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/11/15/PCA/" title="PCA"><img src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="PCA"/></a><div class="content"><a class="title" href="/2021/11/15/PCA/" title="PCA">PCA</a><time datetime="2021-11-15T12:34:48.000Z" title="Created 2021-11-15 20:34:48">2021-11-15</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/10/19/Kernel-Function/" title="核函数"><img src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="核函数"/></a><div class="content"><a class="title" href="/2021/10/19/Kernel-Function/" title="核函数">核函数</a><time datetime="2021-10-19T11:13:40.000Z" title="Created 2021-10-19 19:13:40">2021-10-19</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/10/12/svm-xia/" title="支持向量机（下）"><img src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="支持向量机（下）"/></a><div class="content"><a class="title" href="/2021/10/12/svm-xia/" title="支持向量机（下）">支持向量机（下）</a><time datetime="2021-10-12T00:31:00.000Z" title="Created 2021-10-12 08:31:00">2021-10-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/10/12/svm-shang/" title="支持向量机（上）"><img src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="支持向量机（上）"/></a><div class="content"><a class="title" href="/2021/10/12/svm-shang/" title="支持向量机（上）">支持向量机（上）</a><time datetime="2021-10-12T00:30:53.000Z" title="Created 2021-10-12 08:30:53">2021-10-12</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2021  <i id="heartbeat" class="fa fas fa-heartbeat"></i> Bulua</div></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/HCLonely/images@master/others/heartbeat.min.css"></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="Switch Between Traditional Chinese And Simplified Chinese">繁</button><button id="darkmode" type="button" title="Switch Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="Scroll To Comments"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="Back To Top"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: 'K7ke6VBNMaIjRIWWht9uCbsT-gzGzoHsz',
      appKey: 'rl09GFl8gOiYw6OxIIIuOzur',
      placeholder: 'Please leave your footprints',
      avatar: 'monsterid',
      meta: 'nick,mail,link'.split(','),
      pageSize: '10',
      lang: 'en',
      recordIP: false,
      serverURLs: '',
      emojiCDN: '',
      emojiMaps: "",
      enableQQ: true,
      path: window.location.pathname,
      requiredFields: ["nick,mail"],
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !true) {
  if (true) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script></div><div class="aplayer no-destroy" data-id="186016" data-server="netease" data-type="song" data-fixed="true" data-mini="true" data-listFolded="false" data-order="random" data-preload="none" data-autoplay=true muted></div><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="true" data-click="true"></script><script defer="defer" id="fluttering_ribbon" mobile="true" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = [
  'title',
  '#config-diff',
  '#body-wrap',
  '#rightside-config-hide',
  '#rightside-config-show',
  '.js-pjax'
]

if (false) {
  pjaxSelectors.unshift('meta[property="og:image"]', 'meta[property="og:title"]', 'meta[property="og:url"]')
}

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.removeEventListener('scroll', window.tocScrollFn)
  window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // Analytics
  if (false) {
    MtaH5.pgv()
  }

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>